//
//  KColumnListModule.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 HZMengLiao. All rights reserved.
//

#import "KColumnListModule.h"
#import "KMContext.h"
#import "NSObject+KMModel.h"
#import "KDDCollectionWrapper.h"
#import "KColumnCellView.h"
#import "KColumnBaseLogic.h"

@interface KColumnListModule()

@property (weak,nonatomic) UICollectionView * containerView;

@property (strong,nonatomic) KColumnBaseLogic * logic;

@end

@implementation KColumnListModule

- (instancetype)initWithStorage:(KMStorage *)storage
{
    if (self = [super initWithStorage:storage])
    {
        _logic = [[KColumnBaseLogic alloc] initWithControl:self];
    }
    return self;
}

- (KColumnHorizontalView *)horizontalView
{
    return _logic.horizontalView;
}

- (void)setDelegate:(id<KColumnListModuleDelegate>)delegate
{
    _delegate = delegate;
    _logic.delegate = delegate;
}

- (NSArray<KColumnInfo *> *)columnList
{
    return self.logic.columnList;
}

- (void)reloadHorizontalColumns:(NSArray<KColumnInfo *> *)columnList headerView:(UIView *)headerView toIndex:(NSInteger)toIndex
{
    [_logic reloadHorizontalColumns:columnList headerView:headerView toIndex:toIndex];
}

- (void)reloadVerticalColumns:(NSArray<KColumnInfo *> *)columnList
{
    [_logic reloadVerticalColumns:columnList];
}

- (void)reload
{
    [_logic reload];
}

- (void)stopKVO
{
    [self.logic stopKVO];
}

#pragma mark KColumnListControl Delegate Methods
- (UICollectionView *)containerView
{
    if (_containerView == nil)
    {
        _containerView = self.context.collectionView;
    }
    return _containerView;
}

- (void)loadColumnList:(NSArray<KColumnInfo *> *)columnList
{
    BOOL updateImmediate = self.context.updateImmediate;
    self.context.updateImmediate = YES;
    NSArray<KColumnInfo *> * columnDataList = [self.data.firstObject cellModelList];
    if (columnDataList.count == columnList.count)
    {
        for (KColumnInfo * column in columnList)
        {
            [self configModel:column];
        }
        NSArray<KDDPosition *> * positionList = [KDDPosition cellPositionList:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, columnList.count)] section:0];
        [self.storage modifyModelsAtPositions:positionList withModels:columnList];
    }
    else
    {
        KDDSectionData * sectionData = [[KDDSectionData alloc] init];
        sectionData.cellModelList = columnList;
        sectionData.headerFooterCross = NO;
        [self loadSectionList:@[sectionData]];
//        [self loadModelList:columnList];
    }
    [self.storage.data.firstObject setHeaderFooterCross:NO];
    self.context.updateImmediate = updateImmediate;
    if (columnList.count > 0)
    {
        [self.logic startKVO:self.containerView];
    }
    else
    {
        [self.logic stopKVO];
    }
}

- (void)reloadColumn:(KColumnInfo *)column
{
    KDDPosition * position = [self.storage positionOfModel:column inSection:0];
    if (position != nil)
    {
        [self configModel:column];
        BOOL updateImmediate = self.context.updateImmediate;
        self.context.updateImmediate = YES;
        [self.storage reloadModelsAtPositions:@[position]];
        self.context.updateImmediate = updateImmediate;
    }
}

- (void)columnInfo:(KColumnInfo *)column displayPositionChanged:(KDisplayPosition)position
{
    if ([self.delegate respondsToSelector:@selector(column:displayPositionChanged:)])
    {
        [self.delegate column:column displayPositionChanged:position];
    }
}

- (BOOL)configModel:(KColumnInfo *)model
{
    model.ddConfigID = KColumnCellViewID;
    model.ddViewType = KDDViewTypeCell;
    model.control = self;
    [model refreshItemSize];
    return [super configModel:model];
}

@end
